package com.szzz.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;

/**
 * 全局路由 IP 检查拦截器
 *
 * @author ZhangYin
 */
@Slf4j
@Component
public class IpCheckGlobalFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        URI requestUri = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);

        log.info("{}", requestUri.getPath());
        log.info("{}", requestUri.getHost());
        log.info("{}", requestUri.getPort());

        return chain.filter(exchange).then(Mono.fromRunnable(() -> log.info("aaaaaaaaaaaaa")));
    }

    @Override
    public int getOrder() {
        return Integer.MAX_VALUE;
    }
}
